package cn.com.xjnt.vas.web.ordering.api;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.frameworks.core.web.controller.BaseController;
import com.frameworks.core.web.result.DataResult;

import cn.com.xjnt.vas.manage.domain.entity.Organization;
import cn.com.xjnt.vas.ordering.domain.entity.Department;
import cn.com.xjnt.vas.ordering.domain.entity.EmployeeInfo;
import cn.com.xjnt.vas.ordering.domain.entity.User;
import cn.com.xjnt.vas.ordering.domain.entity.UserInfo;
import cn.com.xjnt.vas.ordering.domain.entity.Ward;
import cn.com.xjnt.vas.ordering.service.DepartmentService;
import cn.com.xjnt.vas.ordering.service.EmployeeInfoService;
import cn.com.xjnt.vas.ordering.service.UserInfoService;
import cn.com.xjnt.vas.ordering.service.UserService;
import cn.com.xjnt.vas.ordering.service.WardService;

/** 
* 类说明：
* @author 作者 LUXIN
* @version 创建时间：2016年5月25日 下午6:41:56 
*/
@Controller
@RequestMapping("/api/user")
public class UserAPI extends BaseController{
    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private WardService wardService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private EmployeeInfoService employeeInfoService;

    @RequestMapping(value = "/add", method = { RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public DataResult add(@RequestBody JSONObject json) {
        DataResult dataRet = new DataResult();
        try {
            String mobile = json.getString("mobile");
            String cardno = json.getString("cardno");
            
            List<User> lists = userService.findByMobile(mobile);
            if(lists.size()==0){
                Organization organization = new Organization();
                int type = 0;
                
                Department department = departmentService.getByCardno(cardno);
                Ward ward = wardService.getByCardno(cardno);
                if(department == null){
                    if(ward == null){
                        dataRet.pushError("智能卡未维护相关信息,请联系管理员");
                        return dataRet;
                    }else{
                        organization = ward.getOrganization();
                        type = 1;
                    }
                }else{
                    organization = department.getOrganization();
                    type = 0;
                }
                //保存user表
                User user = new User();
                user.setMobile(mobile);
                userService.save(user);
                
                switch (type) {
                case 0://职员
                    EmployeeInfo employeeInfo = new EmployeeInfo();
                    employeeInfo.setOrganization(organization);
                    employeeInfo.setDepartment(department);
                    employeeInfo.setUser(user);
                    employeeInfo.setRemark("电视注册");
                    employeeInfo.setRegTime(new Date());
                    employeeInfoService.save(employeeInfo);
                    break;
                case 1://病人
                    UserInfo userInfo = new UserInfo();
                    userInfo.setOrganization(organization);
                    userInfo.setWard(ward);
                    userInfo.setUser(user);
                    userInfo.setRemark("电视注册");
                    userInfo.setRegTime(new Date());
                    userInfoService.save(userInfo);
                    break;
                }
                
                dataRet.pushOk("注册成功");
                dataRet.getExtra().put("state", 0);
            }else{
                dataRet.pushOk("已注册，直接进入菜单列表");
                dataRet.getExtra().put("state", 1);
            }
        } catch (Exception e) {
            dataRet.pushError("手机号注册失败");
            logger.error("手机号注册错误", e);
        }
        return dataRet;
    }
}
 